#include "IPC_ShareMem.h"









void * IPC_ShareMemInit(int key, int maxsize, int *shm_id)
{
    *shm_id = shmget((key_t)key, maxsize, 0666|IPC_CREAT); 
    if(*shm_id == -1)
    {      
        COMLOG_ERROR_LOG("shmget failed\n");
        return NULL;
    }   
    //将共享内存连接到当前进程的地址空间
    void * shm = shmat(*shm_id, 0, 0);
    return shm;
}


int    IPC_ShareMemDeinit(int shmid, void *shm_addr)
{
   shmdt(shm_addr);
   shmctl(shmid, IPC_RMID, 0);   
}


int    IPC_ShareMemRead(void *shm_addr, char *buffer, int len)
{
   if(shm_addr != NULL)
   {
      memcpy(buffer, (char *)shm_addr, len);
      return 0;
   }
   COMLOG_ERROR_LOG("shm read failed\n");
   return -1;
}


int    IPC_ShareMemWrite(void *shm_addr, char *buffer, int len)
{
    if(shm_addr != NULL)
    {
       memcpy(shm_addr, (char *)buffer, len);
       return 0;
    }
    COMLOG_ERROR_LOG("shm write failed\n");
    return -1;
       
}
